package com.yzf.zyfverifycode;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yzf.zyfverifycode.utils.Utils;

import java.util.ArrayList;

import static com.yzf.zyfverifycode.Constants.TAG;

/**
 * 项目名称：FoldingVerifyCode
 * 类描述：验证码、银行、手机号码、时间显示适配器
 * 创建人：Administrator
 * 创建时间：2019年10月1日
 */
public class SMSAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<Sms> mList;

    SMSAdapter(Context context, ArrayList<Sms> list) {
        mContext = context;
        this.mList = list;
    }

    public void refresh(ArrayList<Sms> list) {
        mList = list;//传入list，然后调用notifyDataSetChanged方法
        for(Sms s : mList){
            Log.d(TAG,  s.getVerifycode() + "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        }
        notifyDataSetInvalidated();
    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder;
        if (convertView == null) {
            mHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.sms_info, null, true);
            mHolder.verify_tv = convertView.findViewById(R.id.verify);
            mHolder.sendNum_tv = convertView.findViewById(R.id.sendNum);
            mHolder.time_tv = convertView.findViewById(R.id.time);
            mHolder.isSuccess_tv = convertView.findViewById(R.id.isSuccess);
            mHolder.des_tv = convertView.findViewById(R.id.des);
            convertView.setTag(mHolder);
            convertView.setBackgroundResource(R.drawable.shape_rectangle_with_radius_list_item_bg);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }

        mHolder.verify_tv.setText(mList.get(position).getVerifycode());
        mHolder.sendNum_tv.setText(mList.get(position).getBankName());
        mHolder.time_tv.setText(Utils.stringToLongToDate(mList.get(position).getReceiveTime()));
        mHolder.isSuccess_tv.setText(mList.get(position).getReceverPhoneNo());
        mHolder.des_tv.setText(mList.get(position).getVerifycode());
        return convertView;
    }

    private class ViewHolder {
        private TextView verify_tv;
        private TextView sendNum_tv;
        private TextView time_tv;
        private TextView isSuccess_tv;
        private TextView des_tv;

    }
}
